<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.myfirefly.cn/tag/auth" prefix="auth"%>
<script type="text/javascript">
	var spfTradeContractDataGrid = $('#spfTradeContract-datagrid-json').datagrid({
		gridTitle : '商品房成交合同管理',
	    showToolbar: true,
	    toolbarItem: '',
	    filterThead: false,
	    filterAll: true,
	    showLinenumber: false,
	    sortAll : true,
	    <auth:has funcId="spfTradeContract" action="/spfTradeContract/list.do">
	    dataUrl: 'spfTradeContract/list.do',
	    </auth:has>
	    dataType: 'json',
	    columns: [
	    	{
				name: '',
				label: '操作',
				align: 'center',
				width: 130,
				quicksort:false,
				menu: false,
				edit: false,
				render: function(value){
			        var html = '';
			        <auth:has funcId="spfTradeContract" action="/spfTradeContract/show.do">
				    html += '<div class="btn-group" role="group"><button type="button" class="btn btn-green btn-sm" onclick="showSpfTradeContract(this)"><i class="fa fa-eye"></i> 查看</button></div>';
				    </auth:has>
				    <auth:has funcId="spfTradeContract" action="/spfTradeContract/edit.do">
				    html += '<div class="btn-group" role="group"><button type="button" class="btn btn-blue btn-sm" onclick="editSpfTradeContract(this)"><i class="fa fa-edit"></i> 编辑</button></div>';
				    </auth:has>
				    //html += '<div class="btn-group" role="group"><button type="button" class="btn btn-orange" onclick="delSpfTradeContract(this)"><i class="fa fa-remove"></i> 删除</button></div>';
				    return html;
				}
	        },
	        {
	            name: 'tradeContractStatus',
	            label: '成交合同状态',
	            type : 'select',
	            items: [{'1':'新建'}, {'2':'已录跟踪单'}, {'3':'已传网签合同未审核'}, {'4':'已传网签合同已审核'}, {'5':'已完成'}, {'6':'已取消'}],
	            align: 'center',
	            width: 220,
	            render: $.datagrid.renderItem
	        },
	        {
	            name: 'tradeAddress',
	            label: '成交地址',
	            align: 'center',
	            width: 220
	        },
	        {
	            name: 'tradeArea',
	            label: '面积',
	            align: 'center',
	            width: 80
	        },
	        {
	            name: 'customerName',
	            label: '客户姓名',
	            align: 'center',
	            width: 120
	        },
	        {
	            name: 'contactWay',
	            label: '联系方式',
	            align: 'center',
	            width: 120
	        },
	        {
	            name: 'identifyNum',
	            label: '证件号码',
	            align: 'center',
	            width: 180
	        },
	        {
	            name: 'tradeAmount',
	            label: '成交价',
	            align: 'center',
	            width: 120
	        },
	        {
	            name: 'tradeDate',
	            label: '成交日期',
	            type : 'date',
	            align: 'center',
	            width: 120,
	            render: function(val) {
	            	if(val){
	            		val = dateToStr('yyyy-MM-dd', new Date(val));
	            	}
	            	return val;
	            }
	        },
	        {
	            name: 'tradeRegion',
	            label: '成交区域',
	            align: 'center',
	            width: 120
	        },
	        {
	            name: 'tradeShop',
	            label: '成交店面',
	            align: 'center',
	            width: 120
	        },
	        {
	            name: 'tradePerson',
	            label: '成交人',
	            align: 'center',
	            width: 120
	        },
	        {
	            name: 'belongShopManager',
	            label: '所属店经理',
	            align: 'center',
	            width: 120
	        },
	        {
	            name: 'receivableCommission',
	            label: '应收佣金',
	            type : 'number',
	            align: 'center',
	            width: 120
	        },
	        {
	            name: 'receivedCommission',
	            label: '已收佣金',
	            type : 'number',
	            align: 'center',
	            width: 120
	        },
	        {
	            name: 'receivedDate',
	            label: '收佣日期',
	            type : 'date',
	            align: 'center',
	            width: 120,
	            render: function(val) {
	            	if(val){
	            		val = dateToStr('yyyy-MM-dd', new Date(val));
	            	}
	            	return val;
	            }
	        },
	        {
	            name: 'createBy',
	            label: '创建人',
	            align: 'center',
	            width: 120
	        },
	        {
	            name: 'createDate',
	            label: '创建日期',
	            type : 'date',
	            pattern:'yyyy-MM-dd HH:mm:ss',
	            align: 'center',
	            width: 200,
	            render: function(val) {
	            	if(val){
	            		val = dateToStr('yyyy-MM-dd HH:mm:ss', new Date(val));
	            	}
	            	return val;
	            }
	        }
	    ],
	    afterSave: function($trs, datas) {
	        	this.refresh();
	        },
	    paging: {pageSize:20, selectPageSize:'20,30,40'},
	    showTfoot: true,
	    fullGrid: false,
	    toolbarCustom: function(){
	    	var html = '';
	    	<auth:has funcId="spfTradeContract" action="/spfTradeContract/add.do">
	    	html += '<div class="btn-group" role="group"><button type="button" class="btn btn-blue" onclick="addSpfTradeContract()"><i class="fa fa-plus"></i> 添加</button></div>';
		    </auth:has>
	    	html += '<div class="btn-group" role="group"><button type="button" class="btn btn-green" onclick="refreshSpfTradeContract()"><i class="fa fa-refresh"></i> 刷新</button></div>';
		    return html;
	    }
	});

	try {
		spfTradeContractDataGrid.data('bjui.datagrid').colLock(0, true);
	} catch (e) {
	}
	
	function refreshSpfTradeContract() {
		spfTradeContractDataGrid.datagrid("refresh");
	}

	function addSpfTradeContract() {
		$('#edit-spfTradeContract-dialog').dialog({
			id:'add-spfTradeContract-dialog', 
			url:'spfTradeContract/add.do', 
			maxable: false,
			mask: true,
			width: 850,
			height: 480,
			onLoad: function($dialog){
				setEditSpfTradeContractBtnEvent($dialog, true);
				$("#l_tradeContractStatus").val("新建");
			},
			title:'商品房成交合同管理 - 添加'});
	}
	function editSpfTradeContract(_this) {
		var index = $(_this).closest("tr").index();
		var data = spfTradeContractDataGrid.data('bjui.datagrid').getRowData(index);
		$('#edit-spfTradeContract-dialog').dialog({
			id:'edit-spfTradeContract-dialog',
			url:'spfTradeContract/edit.do',
			maxable: false,
			mask: true,
			width: 850,
			height: 480,
			onLoad: function($dialog){
				setEditSpfTradeContractBtnEvent($dialog, false);
				
				var failCallback = function(result) {
					$dialog.alertmsg('info', result.message);
					$dialog.dialog('closeCurrent');
				}
				getJsonData2Form('spfTradeContract/get.do', 'id='+data.id, $('#spfTradeContract_form'), spfTradeContractCallback, failCallback);
				
			},
			
			title:'商品房成交合同管理 - 编辑'});
	}
	
	function showSpfTradeContract(_this) {
		var index = $(_this).closest("tr").index();
		var data = spfTradeContractDataGrid.data('bjui.datagrid').getRowData(index);
		$('#edit-spfTradeContract-dialog').dialog({
			id:'edit-spfTradeContract-dialog',
			url:'spfTradeContract/show.do?contractId='+data.id,
			maxable: false,
			mask: true,
			width: 850,
			height: 600,
			onLoad: function($dialog){
				var $cancel = $dialog.find('.cancel');
				$cancel.click(function(){
					$dialog.dialog('closeCurrent');
				});
				
				getJsonData2Form('spfTradeContract/getDetail.do', 'id='+data.id, $('#spfTradeContract_form'), spfTradeContractCallback);
			},
			
			title:'商品房成交合同管理 - 成交合同详情'});
	}
	function delSpfTradeContract(_btn){
		var index = $(_btn).closest("tr").index();
		var data = spfTradeContractDataGrid.data('bjui.datagrid').getRowData(index);

		spfTradeContractDataGrid.alertmsg('confirm', '确定删除当前数据？', 
			{
			    displayMode:'slide', 
	            okName:'确定', 
	            cancelName:'取消', 
	            title:'提示信息',
	            okCall : function(){
	            	var url = "spfTradeContract/del.do?id="+data.id;
					var successCallBack = function(){
						refreshSpfTradeContract();
					}
					myAjax(url,	null, null,successCallBack,null);
	            }
	    });
	}
	
	function spfTradeContractCallback($inputObj, _name, _val){
		if (_name == 'tradeContractStatus') {
			if (_val == '1') {
				$("#l_tradeContractStatus").val("新建");
			} else if (_val == '2') {
				$("#l_tradeContractStatus").val("已录跟踪单");
			} else if (_val == '3') {
				$("#l_tradeContractStatus").val("已传网签合同未审核");
			} else if (_val == '4') {
				$("#l_tradeContractStatus").val("已传网签合同已审核");
			} else if (_val == '5') {
				$("#l_tradeContractStatus").val("已完成");
			} else if (_val == '6') {
				$("#l_tradeContractStatus").val("已取消");
			}
		}
	}
	
	function setEditSpfTradeContractBtnEvent($dialog, isAdd) {
		var $save, $cancel;
		$save = $dialog.find('.save');
		$cancel = $dialog.find('.cancel');
	
		$save.click(function(){
			$('#spfTradeContract_form').isValid(function(v) {
				if(v){
					var url = 'spfTradeContract/addSave.do';
					if (!isAdd) {
						url = 'spfTradeContract/editSave.do';
					}
					var data = $('#spfTradeContract_form').serialize();
					var successCallBack = function(){
						$dialog.dialog('closeCurrent');
						refreshSpfTradeContract();
					}
					myAjax(url,	data, true,successCallBack,null);
				}
			});
		});
	
		$cancel.click(function(){
			$dialog.dialog('closeCurrent');
		});
	}
</script>
<div class="bjui-pageContent" id="spfTradeContract-index-pagecontent">
    <div style="padding:0px; height:100%; width:100%;">
        <table id="spfTradeContract-datagrid-json" data-width="100%" data-height="100%" class="table table-bordered">
        </table>
    </div>
</div>
<div id="edit-spfTradeContract-dialog" style="display: none;">
</div>